Matplotlib transformed unixtime in candlesticks_ohlc gives unsupported operand type(s) for - datetime.datetime and float

by: helix05, 9 years ago


Thank you very much for making such valuable python programming resource.
In your matplotlib tutorials you explained how to transform Unix time stamp to better format by using vectorize(dt.datetime.fromtimestamp). After transformation, I was trying to use dates to make a candlesticks chart, but faced the error above (unsupported operand type(s) for -: 'datetime.datetime' and 'float'). Would you please explain how to fix the error.
Thank you.
P.S. I am using Python 2.7    



You must be logged in to post. Please login or register an account.



I'd need to see the code, edit that in or post it.

-Harrison 9 years ago

You must be logged in to post. Please login or register an account.


Hello, again!! Here is the code

Here I am reading data from MetaTrader4 and converting to format like this: datetime.datetime(2016, 3, 24, 2, 15)


[ datep,highp,lowp,closep,openp,volumep=np.loadtxt('C:UsersAndrey GorbunovAppDataRoamingMetaQuotesTerminal50CA3DFB510CC5A8F28B48D1BF2A5702MQL4FilesGBPUSD.csv',
                                               unpack=True,
                                               delimiter=',')

dateconv=np.vectorize(dt.datetime.fromtimestamp)
datep=dateconv(datep)][/]

Next, I am trying to plot a candlestick chart.

[def Visual(datep,highp,lowp,closep,openp,volumep):
    try:
        fig = plt.figure()
        ax1 = plt.subplot2grid((1,1), (0,0))
        x=0
        y=len(datep)
        ohlc=[]
        while x<y:
            append_me=datep[x],openp[x],highp[x],lowp[x],closep[x],volumep[x]
            ohlc.append(append_me)
            x+=1

        candlestick_ohlc(ax1,ohlc)
        plt.show()
    except Exception,e:
        print'main loop',str(e)][/]

,but I am getting the error: main loop unsupported operand type(s) for -: 'datetime.datetime' and 'float'

Thank you for your help.

-helix05 9 years ago

You must be logged in to post. Please login or register an account.


I am guessing it is happening at this line:
datep=dateconv(datep)

I would look into what your datep is, print it out prior to this line to see if it is what you're expecting it to be.

One issue is that datetime.fromtimestamp wants a unix value, ie: float, but you're passing a datetime, hence the error.

-Harrison 9 years ago

You must be logged in to post. Please login or register an account.


I am pretty sure I am feeding unix code:
here is a print from unconverted file:

[] print datep
[  1.45880730e+09   1.45880820e+09   1.45880910e+09   1.45881000e+09
   1.45881090e+09   1.45881180e+09   1.45881270e+09   1.45881360e+09
   ] [/]

and this is the print after datetime.fromtimestamp

[] 2016-03-24 02:15:00
2016-03-24 02:30:00
2016-03-24 02:45:00
2016-03-24 03:00:00
2016-03-24 03:15:00
2016-03-24 03:30:00
2016-03-24 03:45:00
2016-03-24 04:00:00
[/]

lastly, the print after ohlc.append

[(datetime.datetime(2016, 3, 24, 12, 45), 1.4140299999999999, 1.4147700000000001, 1.4137200000000001, 1.41473, 814.0), (datetime.datetime(2016, 3, 24, 13, 0), 1.41475, 1.4155500000000001, 1.4146700000000001, 1.41536, 984.0).......

So....., it looks like it should work but I keep getting the mistake.

-helix05 9 years ago

You must be logged in to post. Please login or register an account.